<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of account
 *
 * @author dian
 */
class Network extends PV_Controller {
    
    function __construct(){
        parent::__construct();
    }
    
    function index($id_member){
        $this->load->model("member_model");
        $this->load->helper("account_helper");
        
        $data = array(
            "page_title" => "Geneologi",
            "member_data" => $this->member_model->get_data_by_id($id_member)
        );
        
        $this->view("network/index", $data);
    }
    
    function geneology_data($id_parent){
        $this->load->model(array("member_model", "network_model"));
        $this->load->helper("account_helper");
        
        $id_member = $this->member_model->get_id_by_alias($id_parent);
        $downlines = $this->network_model->get_downline($id_member);
        $data = array(
            "jml_downline" => $downlines->num_rows(),
            "list_downline" => $downlines->result()
        );
        
        exit($this->load->view("network/geneology_data", $data, true));
    }
    
    function board($id_member){   
        $this->load->model(array("network_model"));
        $data = array(
            "page_title" => "Board"
        );
        $data["id_member"] = $id_member;
        if ($this->network_model->is_expert($id_member)){
            $data["tipe_board"] = "Expert";
        } else {
            $data["tipe_board"] = "Starter";
        }
        $this->view("network/board", $data);
    }
    
    function board_data_alias($id_parent){
        $this->load->model(array("member_model"));
        $this->board_data($this->member_model->get_id_by_alias($id_parent));
    }
    
    function board_data($id_parent){
        $this->load->model(array("member_model", "network_model"));
        $this->load->helper("account_helper");
        $board = $this->network_model->get_board($id_parent);
        
        $self_data = $this->member_model->get_data_by_id($id_parent);
        $node_0 = new stdClass();  
        $node_0->src = _get_profile_picture(_get_raw_object($self_data, "foto_member"), _get_raw_object($self_data, "jenis_kelamin"));
        $node_0->alias_member = _get_raw_object($self_data, "alias_member");
        $node_0->firstname = _get_raw_object($self_data, "firstname");
        $node_0->lastname = _get_raw_object($self_data, "lastname");
        
        $data = array(
            "page_title" => "Board",
            "node_0" => $node_0
        );
        
        for($i = 1; $i <= 6; $i++){
            $other_data = $this->member_model->get_data_by_id(_get_raw_object($board, "node_".$i));
            
            $x = new stdClass();
            $x->src = _get_profile_picture(_get_raw_object($other_data, "foto_member"), _get_raw_object($other_data, "jenis_kelamin"));
            $x->alias_member = _get_raw_object($other_data, "alias_member");
            $x->firstname = _get_raw_object($other_data, "firstname");
            $x->lastname = _get_raw_object($other_data, "lastname");
        
            $data["node_".$i] = $x;
        }
        
        exit($this->load->view("network/board_data", $data, true));
    }
    
    function downline_register(){
        $data = array(
            "page_title" => "Pendaftaran Downline"
        );
        
        $this->view("network/downline", $data);
    }
    
    function downline_activate(){
        $this->load->model(array("member_model", "transaction_model"));
        
        $data = array(
            "page_title" => "Aktivasi Downline"
        );
        
        if(filter_input(INPUT_SERVER, "REQUEST_METHOD") == "POST"){
            $id_downline = $this->input->post("rowid");
            $id_req_pin = $this->input->post("pin");
            
            $is_success = $this->member_model->is_activation_token_valid($this->user->get_id(), $id_downline, $id_req_pin);
        
            if($is_success){
                $this->member_model->update_activation($id_downline);
                $this->transaction_model->update_token($id_req_pin);
                
                redirect("network/downline_activate?success=1");
            } else {
                $data["error_message"] = "PIN tidak valid untuk member yang diaktifasi";
            }
        }
        
        $data["list_downline"] = $this->member_model->get_downline_not_yet_activated($this->user->get_id());
        $data["list_pin_active"] = $this->transaction_model->get_active_pin($this->user->get_id());
        
        $this->view("network/activate", $data);
    }
    
}
